package com.polaris.live.ui.party.fragment.roomInfo

import android.os.Bundle
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R.*
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.FragmentAnchorRoomInfoBinding
import com.polaris.live.interface_bag.XpopupCallBackImpl
import com.polaris.live.net.liveService
import com.polaris.live.popup.party.WelcomeMessagePopup
import com.polaris.live.resp.back_resp.LiveWelcomeResp
import com.polaris.live.resp.back_resp.PartyRoomInfoResp
import com.polaris.live.translation.R
import com.polaris.live.utils.RichCharmImgUtil
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * SpectatorRoomInfoFragment 主播的房间资料
 * @author Created by 天晴 on 2024/10/16 14:42
 * @since 1.0.0
 **/
class AnchorRoomInfoFragment : BaseVbFragment<BaseViewModel, FragmentAnchorRoomInfoBinding>() {

    private var mPartyRoomInfoResp: PartyRoomInfoResp? = null

    override fun initView(savedInstanceState: Bundle?) {
        initListener()
        mPartyRoomInfoResp?.let {
            setDataView(it)
        }
    }

    private fun initListener() {

        mBinding.welcomeCl.setOnClickListener {
            showWelcomeMessagePopup()
        }
    }

    fun setDataView(mPartyRoomInfoResp: PartyRoomInfoResp) {
        this.mPartyRoomInfoResp = mPartyRoomInfoResp
        withLoaded {
            mBinding.apply {
                tvNickname.text = mPartyRoomInfoResp.user.nickname
                val color = RichCharmImgUtil.serColor2HexColor(mPartyRoomInfoResp.user.nicknameColor)
                    ?: ColorUtils.getColor(color.white)
                tvNickname.setTextColor(color)

                avAvatar.setImageUrl(mPartyRoomInfoResp.user.avatar)
                avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(mPartyRoomInfoResp.user.avatarFrame))
                lvLevel.setRichsLevel(mPartyRoomInfoResp.user.richs)
                lvLevel2.setCharmsLevel(mPartyRoomInfoResp.user.charms)
                sexView.setSexAndAge(mPartyRoomInfoResp.user.sex, mPartyRoomInfoResp.user.birthday)
                vipView.setVipLevel(mPartyRoomInfoResp.user.vipLevel)
                if (mPartyRoomInfoResp.user.country.isNullOrEmpty() && mPartyRoomInfoResp.user.secondLanguage.isNullOrEmpty()) {
                    clLl.visibility = BottomPopupView.GONE
                } else {
                    clLl.visibility = BottomPopupView.VISIBLE
                    cvCountry.setCountry(mPartyRoomInfoResp.user.country)
                    languageView.setLanguage(mPartyRoomInfoResp.user.secondLanguage)
                }

                val welcomeStr = mPartyRoomInfoResp.welcomeSpeech ?: StringUtils.getString(R.string.welcome_tips)
                welcomeTv.text = welcomeStr
                mBinding.messageCount.text = "${welcomeStr.length}/250"
            }
        }

    }

    /**
     * 显示WelcomeMessagePopup
     */
    private fun showWelcomeMessagePopup() {
        mViewModel.requestLoading(
            { liveService.welcomeSpeech() }, {
                if (it.allowedUpload) {
                    XPopup.Builder(context)
                        .hasShadowBg(true)
                        .isDestroyOnDismiss(true)
                        .setPopupCallback(object : XpopupCallBackImpl {
                            override fun onCreated(popupView: BasePopupView) {
                            }

                            override fun onDismiss(popupView: BasePopupView) {
                            }
                        })
                        .asCustom(WelcomeMessagePopup(requireContext(), it.content) { msg ->
                            welcomeSpeechUpdate(msg)

                        })
                        .show()
                } else {
                    ToastGravity.showGravity(R.string.welcome_message_edited)
                }

            },
            {
                ToastGravity.showGravity(it.errorMsg)
            }
        )
    }

    private fun welcomeSpeechUpdate(content: String) {
        mViewModel.request(
            { liveService.welcomeSpeechUpdate(LiveWelcomeResp(content)) },
            {
                mBinding.welcomeTv.text = content
                mBinding.messageCount.text = "${content.length}/250"

                ToastGravity.showGravity(R.string.modified_successfully)
            }, {
                ToastGravity.showGravity(it.errorMsg)
            }
        )
    }

}